package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.User;
import ejb.Facade;

/**
 * Servlet implementation class DesinscriprionServlet
 */
@WebServlet("/DesinscriprionServlet")
public class DesinscriptionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DesinscriptionServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public DesinscriptionServlet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	doPost(request, response);
    }
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		User user = (User) request.getSession().getAttribute("user");
		Facade fac = (Facade) request.getSession().getAttribute("Facade");
		//On supprime sa liste d'amis et d'envies
		User userPersit = fac.getUser(user.getIdUser());
		fac.removeUser( userPersit.getIdUser() );
		//Suppression de la session user
		request.getSession().setAttribute("user", null);
		//Redirection
		new IndexServlet(request, response);
		//srequest.getRequestDispatcher("jsp/index.jsp").forward(request, response);
	}

}
